package com.cn.stock.service.Impl;


import com.cn.api.StockTurnoverService;
import com.cn.dto.StockTurnoverDTO;
import com.cn.stock.entity.StockTurnover;
import com.cn.stock.mapper.StockTurnoverMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Component
@RestController
@RequestMapping("/api/stockTurnover")
public class StockTurnoverServiceImpl implements StockTurnoverService {

    @Resource
    StockTurnoverMapper stockTurnoverMapper;

    private StockTurnover stockTurnover = new StockTurnover();

    @Override
    @GetMapping(value = "/findAll")
    public List<StockTurnoverDTO> findAll(@RequestBody StockTurnoverDTO stockTurnoverDTO) {
        stockTurnover.setChange(stockTurnoverDTO.getChange());
        stockTurnover.setChangeType(stockTurnoverDTO.getChangeType());
        stockTurnover.setProductId(stockTurnoverDTO.getProductId());
        stockTurnover.setProductTypeId(stockTurnoverDTO.getProductTypeId());
        return stockTurnoverMapper.findAll(stockTurnover);
    }

    @Override
    @PostMapping(value = "/add")
    public int add(@RequestBody StockTurnoverDTO stockTurnoverDTO) {
        stockTurnover.setChange(stockTurnoverDTO.getChange());
        stockTurnover.setChangeType(stockTurnoverDTO.getChangeType());
        stockTurnover.setProductId(stockTurnoverDTO.getProductId());
        stockTurnover.setProductTypeId(stockTurnoverDTO.getProductTypeId());
        return stockTurnoverMapper.add(stockTurnover);
    }

    @Override
    @DeleteMapping(value = "/delete")
    public int delete(@RequestBody StockTurnoverDTO stockTurnoverDTO) {
        stockTurnover.setChange(stockTurnoverDTO.getChange());
        stockTurnover.setChangeType(stockTurnoverDTO.getChangeType());
        stockTurnover.setProductId(stockTurnoverDTO.getProductId());
        stockTurnover.setProductTypeId(stockTurnoverDTO.getProductTypeId());
        return stockTurnoverMapper.delete(stockTurnover);
    }

    @Override
    @PostMapping(value = "/update")
    public int update(@RequestBody StockTurnoverDTO stockTurnoverDTO) {
        stockTurnover.setChange(stockTurnoverDTO.getChange());
        stockTurnover.setChangeType(stockTurnoverDTO.getChangeType());
        stockTurnover.setProductId(stockTurnoverDTO.getProductId());
        stockTurnover.setProductTypeId(stockTurnoverDTO.getProductTypeId());
        return stockTurnoverMapper.update(stockTurnover);
    }

    @Override
    @PostMapping(value = "/findByProductId")
    public StockTurnoverDTO findByProductId(@RequestParam int productId) {
        return stockTurnoverMapper.findByProductId(productId);
    }
}
